const addressModel = require('../models/addressModel');
const { sendSuccess, sendError } = require('../utils/response');

module.exports = {
  // 添加地址
  async addAddress(req, res) {
    try {
      const { id: userId } = req.user;
      const { receiver_name, receiver_phone, province, city, district, detail_address, is_default } = req.body;
      
      if (!receiver_name || !receiver_phone || !province || !city || !district || !detail_address) {
        return sendError(res, '缺少必要参数', 400);
      }
      
      const addressId = await addressModel.addAddress(userId, {
        receiver_name,
        receiver_phone,
        province,
        city,
        district,
        detail_address,
        is_default
      });
      
      const address = await addressModel.getAddressById(userId, addressId);
      sendSuccess(res, address, '地址添加成功', 201);
    } catch (err) {
      sendError(res, err.message);
    }
  },

  // 更新地址
  async updateAddress(req, res) {
    try {
      const { id: userId } = req.user;
      const { id } = req.params;
      const { receiver_name, receiver_phone, province, city, district, detail_address, is_default } = req.body;
      
      await addressModel.updateAddress(userId, id, {
        receiver_name,
        receiver_phone,
        province,
        city,
        district,
        detail_address,
        is_default
      });
      
      const address = await addressModel.getAddressById(userId, id);
      sendSuccess(res, address, '地址更新成功');
    } catch (err) {
      sendError(res, err.message);
    }
  },

  // 删除地址
  async deleteAddress(req, res) {
    try {
      const { id: userId } = req.user;
      const { id } = req.params;
      
      await addressModel.deleteAddress(userId, id);
      sendSuccess(res, null, '地址已删除');
    } catch (err) {
      sendError(res, err.message);
    }
  },

  // 获取地址列表
  async getAddresses(req, res) {
    try {
      const { id: userId } = req.user;
      const addresses = await addressModel.getUserAddresses(userId);
      sendSuccess(res, addresses);
    } catch (err) {
      sendError(res, err.message);
    }
  },

  // 获取地址详情
  async getAddress(req, res) {
    try {
      const { id: userId } = req.user;
      const { id } = req.params;
      
      const address = await addressModel.getAddressById(userId, id);
      sendSuccess(res, address);
    } catch (err) {
      sendError(res, err.message);
    }
  },

  // 设置默认地址
  async setDefaultAddress(req, res) {
    try {
      const { id: userId } = req.user;
      const { id } = req.params;
      
      await addressModel.setDefaultAddress(userId, id);
      const address = await addressModel.setDefaultAddress(userId, id);
      sendSuccess(res, address, '默认地址设置成功');
    } catch (err) {
      sendError(res, err.message);
    }
  },
  
  async getDefaultAddress(req,res){
    try {
      const { id: userId } = req.user;
    

      
      const address = await addressModel.getDefaultAddress(userId);
      sendSuccess(res, address);
    } catch (err) {
      sendError(res, err.message);
    }
  }
};